package com.tools.sqlmaker.make;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.tools.sqlmaker.factory.SqlMakerFactory;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author: zhoubin
 * @date: 2023/9/7
 */
public abstract class BaseMaker {

    /**
     * 数据库类型，MYSQL ORACLE
     */
    private String dataBase;

    /**
     * 拼装字符串
     * @return
     */
    public List<String> sqlMake() {

        List<String> sqlList = new ArrayList<>();

        for (Map<String, Object> map : getParamList()) {
            String sql = SqlMakerFactory.sqlMake(getDataBase()).makeSingleSql(map, getTableName());

            sqlList.add(sql);
        }

        return sqlList;
    }

    /**
     * 获取参数列表
     * @return
     */
    protected abstract List<Map<String, Object>> getParamList();

    /**
     * 默认的map
     * @return
     */
    protected abstract Map<String, Object> makeDefaultMap();

    /**
     * 获取SQL模版
     * @return
     */
    protected abstract String getTableName();

    public void setDataBase(String dataBase) {
        this.dataBase = dataBase;
    }

    public String getDataBase() {
        return dataBase;
    }

    private static int seq = 0;

    private String getSeq() {
        if (seq >= 999) {
            seq = 0;
        }
        return StrUtil.fillBefore(String.valueOf(seq++), '0', 3);
    }

    protected String idMake() {
        String format = DateUtil.format(new Date(), "yyMMddHHmmss") + getSeq();
        return format;
    }
}
